﻿using Model.Enums;
using SanZhan.Models.User;

namespace Model.Battle
{
    public class Battle
    {
        public BattleInfo info { get; set; }

        public List<UserWuJiang> wujiangs { get; set; } = new List<UserWuJiang>();


        public Battle(BattleInfo battleInfo)
        {
            info = battleInfo;
        }

        public void Fight()
        {
            info.logs.Add("战斗初始化");
            RunInit();
            info.logs.Add("准备回合");
            RunReady();

            info.RoundNUM = 1;
            while (info.RoundNUM < 9)
            {
                RunAction();
                info.RoundNUM++;
            }

            if (!RunRounds())
            {
                return;
            }
            ;
            RunDraw();
        }

        private void RunInit()
        {

            if (info.attacker.ZhuJiang.Bingli > 0)
            {
                info.attacker.ZhuJiang.Alive = true;
                wujiangs.Add(info.attacker.ZhuJiang);
            }
            if (info.attacker.FuJiang1.Bingli > 0)
            {
                info.attacker.FuJiang1.Alive = true;
                wujiangs.Add(info.attacker.FuJiang1);
            }
            if (info.attacker.FuJiang2.Bingli > 0)
            {
                info.attacker.FuJiang2.Alive = true;
                wujiangs.Add(info.attacker.FuJiang2);
            }

            if (info.defender.ZhuJiang.Bingli > 0)
            {
                info.defender.ZhuJiang.Alive = true;
                wujiangs.Add(info.defender.ZhuJiang);
            }
            if (info.defender.FuJiang1.Bingli > 0)
            {
                info.defender.FuJiang1.Alive = true;
                wujiangs.Add(info.defender.FuJiang1);
            }
            if (info.defender.FuJiang2.Bingli > 0)
            {
                info.defender.FuJiang2.Alive = true;
                wujiangs.Add(info.defender.FuJiang2);
            }

            if (wujiangs == null)
            {
                info.logs.Add("没有武将可以上阵");
                return;
            }
            info.logs.Add($"共{wujiangs.Count}名武将上阵作战");
        }
        private void RunReady()
        {
            //sort with attacker
            info.logs.Add("士气");
            RunShiQi();
            info.logs.Add("兵种适性");
            RunBingZhongShiXing();
            info.logs.Add("主城建筑");
            RunJianzhu();
            info.logs.Add("装备");
            RunZhuangbeiTeji();
            info.logs.Add("列传称号");
            RunLiezhuan();
            //sort with speed
            info.logs.Add("兵书");
            RunBingshu();
            info.logs.Add("被动");
            RunBeidong();
            info.logs.Add("阵法");
            RunZhenfa();
            info.logs.Add("兵种");
            RunBingzhong();
            info.logs.Add("指挥");
            RunZhihui();
        }


        private void RunZhihui()
        {
            //按速度发动，指挥
        }
        private void RunBingzhong()
        {
            //按速度发动，阵法

        }
        private void RunZhenfa()
        {

        }
        private void RunBeidong()
        {
            //按速度发动，武器特技算被动

        }
        private void RunBingshu()
        {
            //按速度

        }
        private void RunLiezhuan()
        {

        }
        private void RunZhuangbeiTeji()
        {
            //按攻队防队顺序

        }
        private void RunJianzhu()
        {
            //按攻方防方顺序，建筑包括：兵战、阵营属性、协力、兵种营、满级特效

        }

        private void RunBingZhongShiXing()
        {
            //按攻防主副顺序

            SetBingZhongShiXing(info.attacker.ZhuJiang);
            SetBingZhongShiXing(info.attacker.FuJiang1);
            SetBingZhongShiXing(info.attacker.FuJiang2);
            SetBingZhongShiXing(info.defender.ZhuJiang);
            SetBingZhongShiXing(info.defender.FuJiang1);
            SetBingZhongShiXing(info.defender.FuJiang2);
        }

        private void SetBingZhongShiXing(UserWuJiang wujiang)
        {
            if (wujiang == null || wujiang.ID <= 0)
            {
                return;
            }
            Bingzhong bingzhong = wujiang.UserTeam.Bingzhong;
            string bingzhongmark = bingzhong.tomark();
            Shixing shixing = wujiang.heroBase.BingzhongShixing.Values[bingzhong];
            string shixingmark = shixing.tomark();
            decimal value = shixing.tovalue();
            string persent = (value * 100M).ToString("0.0");
            wujiang.WuliNow *= value;
            wujiang.ZhiliNow *= value;
            wujiang.TongshuaiNow *= value;
            wujiang.SuduNow *= value;
            wujiang.ZhengzhiNow *= value;
            wujiang.MeiliNow *= value;
            info.logs.Add($"[{wujiang.heroBase.Name}]的【{bingzhongmark}】兵种适性为【{shixingmark}】，属性调整为原来的{persent}%（武力{wujiang.WuliNow.ToString("0.00")}智力{wujiang.ZhiliNow.ToString("0.00")}统帅{wujiang.TongshuaiNow.ToString("0.00")}速度{wujiang.SuduNow.ToString("0.00")}政治{wujiang.ZhengzhiNow.ToString("0.00")}魅力{wujiang.MeiliNow.ToString("0.00")}）");
        }

        private void RunShiQi()
        {
            //按攻防顺序
            SetShiQi(info.attacker);
            SetShiQi(info.defender);
        }
        private void SetShiQi(UserTeam team)
        {
            string txt = "不变";
            if (team.ShiQi < 100)
            {
                int shiqi = team.ShiQi;
                decimal value = (100 - shiqi) * 0.7M;
                team.ZhuJiang.ZaoChengBingRenShangHai = 100 - value;
                team.ZhuJiang.ZaoChengCeLueShangHai = 100 - value;
                team.FuJiang1.ZaoChengBingRenShangHai = 100 - value;
                team.FuJiang1.ZaoChengCeLueShangHai = 100 - value;
                team.FuJiang2.ZaoChengBingRenShangHai = 100 - value;
                team.FuJiang2.ZaoChengCeLueShangHai = 100 - value;
                txt = $"降低{value.ToString("0.00")}%";
            }
            info.logs.Add($"[{team.ZhuJiang.heroBase.Name}]队当前士气值为{team.ShiQi.ToString("0.00")}，造成伤害{txt}");
        }

        private bool RunRounds()
        {

            info.RoundNUM = 1;
            while (info.RoundNUM < 9)
            {
                RunAction();
                info.RoundNUM++;
            }
            return true;
        }
        private void RunSelect()
        {
            OnSelectB();
            OnSelect();
            OnSelectE();
        }
        private void RunAction()
        {
            OnActionB();
            OnAction();
            OnActionE();
        }
        private void RunOP()
        {

        }
        private void RunTarget()
        {
            OnTargetB();
            OnTarget();
            OnTargetE();
        }
        private void RunDraw()
        {
            OnDarw();
        }


        //回合
        public void OnRoundB()
        {
            info.logs.Add($"第{info.RoundNUM}回合 开始前");

            foreach (var item in wujiangs)
            {
                item.Action = true;
            }
        }
        public void OnRound()
        {

            string intent = string.Empty;

            while (true)
            {
                RunSelect();

                RunAction();
            }

        }
        public void OnRoundE() { }

        public void OnSelectB()
        {
            /*
            CurrIntent = "";
            */
        }
        public void OnSelect()
        {
            /*
            var trygetwujiang = wujiangs.OrderBy(x => x.Sudu).FirstOrDefault(x => x.Action && x.Alive);
            if (trygetwujiang == null)
            {
                throw new Exception("No Target");
            }
            CurrWuJiang = trygetwujiang;
            */
        }
        public void OnSelectE()
        {
            /*
            CurrIntent = "------";
            info.logs.Add($"{CurrIntent}[{CurrWuJiang.heroBase.Name}]开始行动");
            */
        }

        //行动
        public void OnActionB() { }

        public void OnAction()
        {

        }
        /*
        public void OnAction()
        {
            RunOP();
            RunTarget();
            //普通攻击
            var target = wujiangs.Where(x => x.Alive && x.Attack != CurrWuJiang.Attack).OrderBy(x => Guid.NewGuid()).First();
            infos.Add($"{CurrIntent}[{CurrWuJiang.heroBase.Name}]对[{target.heroBase.Name}]发动普通攻击");
            //info = CommonAttack(wujiang, target);
            //infos.Add($"{CurrIntent}{info}");

            if (target.Bingli == 0)
            {
                target.Alive = false;
                infos.Add($"{CurrIntent}[{target.heroBase.Name}]兵力为0，无法再战");
                if (target.IsMain)
                {
                    if (target.Attack)
                    {
                        infos.Add($"攻击方的主将[{target.heroBase.Name}]兵力为0，无法再战，防守方胜利");
                    }
                    else
                    {
                        infos.Add($"防守方的主将[{target.heroBase.Name}]兵力为0，无法再战，攻击方胜利");
                    }
                    return;
                }
            }

            CurrWuJiang.Action = false;

        }
        */

        public void OnActionE() { }

        public void OnTargetB() { }
        public void OnTarget() { }
        public void OnTargetE() { }

        private void OnDarw()
        {
            info.logs.Add("平局！");
        }
    }
}
